<?php
class init{
	public $CI=null;
	function __construct(){
	  	$this->CI=&get_instance();
	 }
	function controller(){
		$data['controller']=substr_replace(strtolower($this->CI->uri->rsegment(1,'chiefly')),'',0,3);
		$data['func']=strtolower($this->CI->uri->rsegment(2,'index'));
		$data['load']=&$this->CI->load;
		$data['host']=reset(explode('.',$this->CI->input->server('HTTP_HOST')));
		$hcs=intval($this->CI->input->get('hcs'));
		$this->CI->load->model('catalog');
		if($data['host']==='manage'){
			session_start();
			define('LOGIN',empty($_SESSION['username'])?false:true);
			if(LOGIN===true){
				$data['username']=$_SESSION['username'];
			}
			$hcs=reset($this->CI->catalog->get(array('where'=>array('id'=>$hcs,'parent'=>0,'locked'=>0))));
			if(empty($hcs)){
				$data['htdocs']=false;
			}else{
				$data['htdocs']=array('id'=>$hcs['id'],'name'=>$hcs['name'],'tag'=>$hcs['value']);
				$catalog='manage.'.$data['htdocs']['tag'];
				$data['htdocs']['host']=$this->CI->config->item('base_url');
				$data['htdocs']['host']=rtrim($data['htdocs']['host'],'/').'/';
			}
		}else{
			$htdocs=$this->CI->catalog->get(array('where'=>array('catalog'=>'htdocs','locked'=>0),'order'=>array('parent'=>'ASC')));
			foreach($htdocs as $value){
				if($hcs==$value['id']){
					$data['htdocs']=array('id'=>$value['id'],'name'=>$value['name'],'tag'=>$value['value']);
				}
			}
			if(empty($data['htdocs'])){
				$data['htdocs']=reset($htdocs);
				$data['htdocs']=array('id'=>$data['htdocs']['id'],'name'=>$data['htdocs']['name'],'tag'=>$data['htdocs']['value']);
			}
			$catalog=$data['htdocs']['tag'].'.menu';
		}
		$menu=$this->CI->catalog->get(array('select'=>'id,name,value,parent','where'=>array('catalog'=>$catalog,'locked'=>0),'order'=>array('parent'=>'ASC','sequence'=>'DESC')));
		foreach($menu as $key=>$value){
			$value['value'].=(strpos($value['value'],'?')===false?'?':'&').'clg='.$value['id'];
			$data['menu'][$value['id']]=$value;
		}
		$clg=intval($this->CI->input->get('clg'));
		if(empty($clg)){
			$clg=reset($data['menu']);$clg=$clg['id'];
			$data['caller']['clg'][]=$clg;
		}else{
			while(!empty($data['menu'][$clg])){
				$data['caller']['clg'][]=$clg;
				$clg=$data['menu'][$clg]['parent'];
			}
			$data['caller']['clg']=array_reverse($data['caller']['clg']);
		}
		$data['nav']=build($data['menu'],$data['htdocs']['id']);
		$this->CI->load->vars($data);
	 }
}
?>